const Controller = require('egg').Controller;
class User extends Controller{
    async index(){//get
        var model= this.ctx.model.User;
        var pagesize=this.ctx.query.pagesize|| 10;
        var pageno=this.ctx.query.pageno || 1;
        var sort=this.ctx.query.sort || -1;
        var name=this.ctx.query.name || '';
        var filter={
            userName:new RegExp(name,'ig')
        }
        var doc=await model.find(filter).limit(pagesize).skip((pageno-1)*pagesize).exec();
        var result=await model.find(filter).exec()
        this.ctx.body={
            list:doc,
            total:result.length,
            pageNo:pageno
        };
        //this.ctx.body=await model.find({});
    }
    async create (){//post /user
        var model= this.ctx.model.User;
        //userName password state createTime
        this.ctx.body=await new model(this.ctx.request.body).save();
        
    }
    async show(){// get   /user/:id
        var model= this.ctx.model.User;
        this.ctx.body=await model.find({_id:this.ctx.params.id});       
    }
    async destroy(){//post /user/:id
        var model= this.ctx.model.User;
        //console.log(this.ctx.params)
        this.ctx.body=await model.remove({_id:this.ctx.params.id})
    }
    async update(){//put /user/:id
        var model= this.ctx.model.User;
        this.ctx.body=await model.updateOne({
            _id:this.ctx.params.id
        },this.ctx.request.body)
    }
}
module.exports=User;